<template>
  <el-dialog
    :visible.sync="visible"
    width="400px"
    title="保存方案"
    :before-close="handle_close"
    append-to-body
  >
    <el-form ref="form" :model="form" :rules="formRule" label-width="90px" size="small">
      <el-form-item label="方案名称:" prop="logName">
        <el-input v-model="form.logName" clearable style="width: 260px" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="年度:" prop="year">
        <el-date-picker
          :disabled="logId!==null"
          v-model="form.year"
          type="year"
          value-format="yyyy"
          placeholder="选择年份"
          style="width: 260px"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center">
      <el-button @click="handle_close" size="small">取消</el-button>
      <el-button type="primary" @click="handle_submit" size="small">保存</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  data(){
    return {
      visible: false,
      form: {
        logName: '',
        year: '',
      },
      logId: null,
      formRule: {
        logName: [{required: true, message: "必填", trigger: "change"}],
        year: [{required: true, message: "必选", trigger: "change"}],
      }
    }
  },
  methods: {
    init(val){
      this.visible = true;
      if(val && val.logId !== null){
        this.logId = val.logId;
        this.form.logName = val.logName;
        this.form.year = val.year;
      }
    },
    handle_close(){
      this.visible = false;
    },
    async handle_submit(){
      await this.$refs.form.validate();
      let form;
      if(this.logId) form = { ...this.form, logId: this.logId };//更新
      else form = { ...this.form };//新增
      this.$emit('save', form);
    }
  }
}
</script>
<style lang="scss" scoped>

</style>